﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.新增窗体;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.出库管理.原料出库
{
    public partial class 原料领用计划列表 : Form
    {
        public 原料领用计划列表()
        {
            InitializeComponent();
            this.gridControl1.ContextMenuStrip = this.contextMenuStrip1;
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.DateTime = dateEdit2.DateTime.AddDays(-QueryTime.间隔);
            CreateGrid.Create(this.Name, gridView1);
            Query();
            gridView1.OptionsCustomization.AllowSort = true;
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess(this.Name) == true)
            {
                MainForm.mainform.AddMidForm(new 原料领用计划单(FormUseful.新增));
            }
        }
        public virtual void Query()
        {
            var dt = Connect.dbcontext.Queryable<DanjuTable, ImportAndExportOfRawMaterials>((x,y)=>(x.dh==y.DocNum)).Where((x,y)=>x.rq>=dateEdit1.DateTime.Date
            &&x.rq<=dateEdit2.DateTime.Date.AddDays(1)&&y.ProductName.Contains(txtpingming.Text )&&x.djlx==DanjuLeiXing.原料领用计划单).Select((x,y)=>new DataTable()).ToDataTable();
            gridControl1.DataSource = dt;
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改原料领用计划单") == true)
            {
                if (gridView1.FocusedRowHandle >= 0)
                {
                    if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                    {
                        MainForm.mainform.AddMidForm(new 原料领用计划单(FormUseful.修改) { danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                    }
                    else
                    {
                        if (SysInfo.GetInfo.own != "审核制")
                        {
                            MainForm.mainform.AddMidForm(new 原料领用计划单(FormUseful.修改) { danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                        }
                        else
                        {
                            MessageBox.Show("对不起!改单据已经审核，如需修改请先取消审核状态！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }
            }
           
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("删除原料领用计划单"))
            {
                if (gridView1.FocusedRowHandle >= 0)
                {
                    if ((int)(MessageBox.Show("您确定要删除改原料领用计划单吗？确定按YES.取消按NO", "提示", MessageBoxButtons.YesNo)) == 6)
                    {
                        try
                        {
                            Connect.dbcontext.BeginTran();
                            原料领用BLL.原来领用计划单删除单据(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                            Connect.dbcontext.CommitTran();
                            Query();
                        }
                        catch (Exception ex)
                        {
                            Connect.dbcontext.RollbackTran();
                            ShowMessageBox.ShowErrorBox("删除时发送了错误！" + ex.Message);

                        }
                    }
                }
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "原料领用计划单清单");
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void 审核通过ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("审核原料领用计划单"))
            {
                if (gridView1.FocusedRowHandle >= 0)
                {
                    if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                    {
                        采购入库单BLL.单据审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                        AlterDlg.Show("审核成功！");
                        Query();
                    }
                    else
                    {
                        MessageBox.Show("该单据已经审核过了！无需再次审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void 单据反审ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("反审核原料领用计划单"))
            {
                if (gridView1.FocusedRowHandle >= 0)
                {
                    if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()))
                    {
                        采购入库单BLL.单据反审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                        AlterDlg.Show("反审核成功！");
                        Query();
                    }
                    else
                    {
                        MessageBox.Show("该单据还未审核通过！不能反审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = null, dt = gridControl1.DataSource as DataTable };
            fm.ShowDialog();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }
    }
}
